package com.etc.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.etc.pojo.EmpPo;

// bean一下
@Controller
public class EmpController {

	// 匹配get请求 init.action
	// 找到handlermapping----> 返回handler
	@RequestMapping(value="init.action", method=RequestMethod.GET)
	public ModelAndView init() {
		
		List<String> jlist = Arrays.asList("经理", "销售", "人事");
		
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("search"); // 视图解析器 / search .jsp
		mav.addObject("jlist", jlist); // request.setAttribute("jlist", jlist)
		
		return mav;
	}
	
	@RequestMapping("init1.action")
	public String init1(HttpServletRequest req) {
		
		List<String> jlist = Arrays.asList("经理", "销售", "人事");
		req.getServletContext().setAttribute("jlist", jlist); // request.setAttribute("jlist", jlist)
		
		return "search";
	}
	
	// search.action?ename=input&job=select
/*	@RequestMapping("search.action")
	public String search(@RequestParam("ename") String ename,String job,   HttpServletRequest req) {

//		String ename = req.getParameter("ename");
//		String job = req.getParameter("job");
		
		System.out.println(ename);
		System.out.println(job);
		return "search";
	}*/
	
	@RequestMapping("search.action")
	public String search(EmpPo po, Model model) {
		
		System.out.println(po.getEname());
		System.out.println(po.getJob());
		
		List<EmpPo> elist = new ArrayList<>();
		elist.add(new EmpPo(111, "tom", "经理"));
		elist.add(new EmpPo(112, "jeery", "财务"));
		elist.add(new EmpPo(113, "jhone", "财务"));
		
		model.addAttribute("elist", elist);
		
		
		return "search";
	}
}
